package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {


    @Autowired
    private CartService cartService;

    @Autowired
    private TokenDecode tokenDecode;
    /**
     *添加购物车
     * @param skuId 商品id，用于查询商品具体数据
     * @param num 数量
//     * @param username 用户名，根据用户名来确定谁的购物车
     */
    @GetMapping("/add")
    public Result addCart(@RequestParam("skuId") String skuId, @RequestParam("num")Integer num){
        String username = tokenDecode.getUserInfo().get("username");
        cartService.addCart(skuId,num,username);
        return new Result(true, StatusCode.OK,"添加购物车成功");
    }


    /**
     * 查看商品购物车
     * @return
     */
    @GetMapping("/list")
    public Map list(){

        String username = tokenDecode.getUserInfo().get("username");
        Map map = cartService.list(username);
        return map;
    }


}
